# Some notes:
# Updated by Wei 2013/07/13 night (do not know how to define the label so pause into a later case)
# Updated by Wei 2013/06/10 night
# Updated by Wei 2013/02/22 night
# should add the different kinds of labels here, but not on the final stage to add the labels. :)

# Program inputs:
# option1 for the machine pangolin
# (1) inputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_01_10.train_tfqAdded"
# option2 for the machine dodo
# (1) inputFileName = "/home/diaosi/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_02_21.smallTest.train_tfqAdded"

# Program outputs:
# option1 for the machine pangolin
# (1) outputFileName = "/data3/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_01_10.train_tfqAdded_labelsAdded"
# option2 for the machine dodo
# (1) outputFileName = "/home/diaosi/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_01_10.train_tfqAdded_labelsAdded"

# Program Purpose:
# PURPOSE: add a classification label into each training instance, 
# HOW to compute: it is computed from the field rank_in_this_results_list_for_this_query
# The current classification label scheme (Updated by Wei 2013/02/21) The name is called: scheme2
# TOP10, NotTop10

# The OLD classification label schema is the following(This can be easily changed):
# Updated by Wei 2013/02/21. The name is called: scheme1
# TOP10, TOP11To50, TOP51To100

def assignClassLabelBasedOnRankInTheResultsList(rank_in_this_results_list_for_this_query, scheme_num):
    classificationLabel = "None"
    if scheme_num == 1:
        if rank_in_this_results_list_for_this_query <= 10:
            classificationLabel = "TOP10"
        elif rank_in_this_results_list_for_this_query >10 and rank_in_this_results_list_for_this_query <= 50:
            classificationLabel = "TOP11To50"
        elif rank_in_this_results_list_for_this_query >50 and rank_in_this_results_list_for_this_query <= 100:
            classificationLabel = "TOP51To100"    
        else:
            print "system error, no such label, the rank is:",rank_in_this_results_list_for_this_query
            classificationLabel = "None"
    elif scheme_num == 2:
        if rank_in_this_results_list_for_this_query <= 10:
            classificationLabel = "TOP10"
        else:
            classificationLabel = "NotTOP10"
    elif scheme_num == 3:
        if rank_in_this_results_list_for_this_query <= 100:
            classificationLabel = "TOP100"
        else:
            classificationLabel = "NotTOP100"        
    else:
        print "system error, scheme_num NOT supported:",scheme_num
    
    return classificationLabel


print "Program Begins..."
# option1:
inputFileName = "/home/diaosi/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_06_09.smallTest.Combine.train_tfqAdded"
# option2:
# inputFileName = "/home/diaosi/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_02_22.smallTest.train_tfqAdded"
inputFileHandler = open(inputFileName,"r")
infoHeadLine = inputFileHandler.readline()
newInfoHeadLine = infoHeadLine.strip() + " " + "relevenceLabel" + "\n"
print "newInfoHeadLine:",newInfoHeadLine

# option1:
outputFileName = "/home/diaosi/gov2ClearYourMindAndDoItAgain/Posting_Oriented_Training_Dataset_2013_06_09.smallTest.Combine.train_tfqAdded_labelsAdded"
outputFileHandler = open(outputFileName,"w")
outputFileHandler.write(newInfoHeadLine)


# print "len(inputFileHandler.readlines()):",len(inputFileHandler.readlines())

for line in inputFileHandler.readlines():
    newOutputLine = ""
    lineElements = line.strip().split(" ")
    # for debug test
    # -2 is the index for the rank in the result list
    # print "lineElements[-2]:",lineElements[-2]
    # print "lineElements:",lineElements
    rank_in_this_results_list_for_this_query = int( lineElements[-2] )
    
    classificationLabel = "None"
    classificationLabel = assignClassLabelBasedOnRankInTheResultsList(rank_in_this_results_list_for_this_query,3)
    
    newOutputLine = line.strip() + " " + classificationLabel + "\n"
    outputFileHandler.write(newOutputLine)
    

inputFileHandler.close()
outputFileHandler.close()
print "Program Ends."

